<%@ page import="java.util.*" %>
<%@ page import="java.io.File" %>
<%@ page import="java.net.*" %>
<%@ page import="org.w3c.dom.*, javax.xml.parsers.*" %>

<jsp:useBean id="cart" scope="session" class="sklep.Koszyk" />
<html>
	<head>
		<title>Shop Catalog</title>
	</head>
	<body>
		<%
		String username =  (String)session.getAttribute("username");  
 		if(username == null)
  		{ 
			response.sendRedirect("login.jsp");
  		}
		cart.checkRequest(request);
		%>
		<a href="koszyk.jsp">Shopping Cart Quantity:</a>
			<p><%=cart.getNumOfItems() %></p>
		<a href="logout.jsp" id="wyloguj" >wyloguj</a>
		<hr>
		<div align="center"><h3>Items Catalog</h3></div>
		<div align="center">
		<%
		DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
		DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
		URL url = getClass().getClassLoader().getResource("items_data.xml");
		File file = new File(url.getPath());
		Document doc = docBuilder.parse(file);
		Element  element = doc.getDocumentElement();
		
		NodeList parts = element.getElementsByTagName("part");
		
		for (int i = 0; i < parts.getLength(); ++i)
		{
			Element e1 = (Element)parts.item(i);
			NodeList parts_info = e1.getChildNodes();
			%>
			<form action="main.jsp" method="post">
				<div><%=parts_info.item(0).getTextContent() %>, cena:  <%=parts_info.item(1).getTextContent() %></div>
				<input type="submit" name="Submit" value="Add" id="Add<%=e1.getAttribute("ID") %>">
				<input type="submit" name="Submit" value="Remove" id="Remove<%=e1.getAttribute("ID") %>">
				<input type="hidden" name="id" value=<%=e1.getAttribute("ID") %>>
				<input type="hidden" name="product_name" value="<%=parts_info.item(0).getTextContent() %>">
				<input type="hidden" name="price" value="<%=parts_info.item(1).getTextContent() %>">
				<hr>
			</form>
			<%
		}
    	%> 
		</div>
	</body>
</html>
